package com.aidex.common.utils.sms;

import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

public class ChuangLanSmsClient {
    public static String sendSmsByPost(String path, String postContent) {
        URL url = null;
        try {
            url = new URL(path);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.setReadTimeout(10000);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestProperty("Charset", "UTF-8");
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            httpURLConnection.connect();
            OutputStream os = httpURLConnection.getOutputStream();
            os.write(postContent.getBytes("UTF-8"));
            os.flush();
            StringBuilder sb = new StringBuilder();
            int httpRspCode = httpURLConnection.getResponseCode();
            if (httpRspCode == HttpURLConnection.HTTP_OK) {
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
                String line = null;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                br.close();
                return sb.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static JSONObject sendSms(String phone) {
        String sendUrl = "https://smssh1.253.com/msg/v1/send/json";
        Map map = new HashMap();
        map.put("account", "N927180_N7404742");//API账号
        map.put("password", "83g1F35d787b5b");//API密码
        map.put("msg", "【碟金】温馨提示：您有一条新线索 请登录系统查看");//短信内容
        map.put("phone", phone);//手机号
        map.put("report", "true");//是否需要状态报告
        map.put("extend", "356079");//自定义扩展码
        JSONObject js = (JSONObject) JSONObject.toJSON(map);
        return JSONObject.parseObject(sendSmsByPost(sendUrl, js.toString()));

    }

    public static JSONObject sendVariableSms(String params) {
        String sendUrl = "https://smssh1.253.com/msg/variable/json";
        Map map = new HashMap();
        map.put("account", "N648620_N7404742");//API账号
        map.put("password", "37YDq1IXPH459b");//API密码
        map.put("msg", "【车优融】尊敬的{$var}，您好！您的预约已登记成功，请您于{$var}前上门办理业务，预约号{$var}，我们将竭诚为您服务。");//短信内容
        map.put("params", params);//手机号
        map.put("report", "true");//是否需要状态报告
        map.put("extend", "001");//自定义扩展码
        JSONObject js = (JSONObject) JSONObject.toJSON(map);
        return JSONObject.parseObject(sendSmsByPost(sendUrl, js.toString()));
    }

    /**
     * 联系客户短信模板
     *
     * @param params
     * @return
     */
    public static JSONObject sendContactSms(String params) {
        String sendUrl = "https://smssh1.253.com/msg/variable/json";
        Map map = new HashMap();
        map.put("account", "N648620_N7404742");//API账号
        map.put("password", "37YDq1IXPH459b");//API密码
        map.put("msg", "【车优融】尊敬的{$var}，您好！您的预约已登记成功，请您于{$var}前上门办理业务，预约号{$var}，我们将竭诚为您服务。");//短信内容
        map.put("params", params);//手机号
        map.put("report", "true");//是否需要状态报告
        map.put("extend", "001");//自定义扩展码
        JSONObject js = (JSONObject) JSONObject.toJSON(map);
        return JSONObject.parseObject(sendSmsByPost(sendUrl, js.toString()));

    }


    public static JSONObject sendOthersSms(String params, String msg) {
        String sendUrl = "https://smssh1.253.com/msg/variable/json";
        Map map = new HashMap();
        map.put("account", "N648620_N7404742");//API账号
        map.put("password", "37YDq1IXPH459b");//API密码
        map.put("msg", msg);//短信内容
        map.put("params", params);//手机号
        map.put("report", "true");//是否需要状态报告
        map.put("extend", "001");//自定义扩展码
        JSONObject js = (JSONObject) JSONObject.toJSON(map);
        return JSONObject.parseObject(sendSmsByPost(sendUrl, js.toString()));

    }


    public static void main(String[] args) throws ParseException {
        String params = "17771704470,1234214,2024-09-12 18:00";
        JSONObject jsonObject = sendOthersSms(params, "【车优融】系统任务{$var}当前状态异常请及时处理，时间：{$var}");
        System.out.println(jsonObject.getString("code") + jsonObject.getString("msgId") + "/" + jsonObject.getString("errorMsg"));
        SimpleDateFormat simpleDateFormats = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
        SimpleDateFormat simpleDateFormats1 = new SimpleDateFormat("yyyy-MM-DD HH:mm");
        System.out.println("2024-09-12 18:09:07".substring(0, 16));
    }
}
